package leetcode.dfs;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
 * @Author Ammar
 * @Create 2023/7/19 15:01
 */
public class _17_电话号码的字母组合 {

    private char[][] lettersArray = {
            {'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'},
            {'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', 'q', 'r', 's'},
            {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}
    };

    public List<String> letterCombinations(String digits) {
        if (digits == null) return null;
        char[] chars = digits.toCharArray();
        List<String> result = new ArrayList<>();
        if (chars.length == 0) return result;
        char[] string = new char[chars.length];
        dfs(0, result, chars, string);
        return result;
    }

    private void dfs(int index, List<String> result, char[] chars, char[] string) {
        if (index == chars.length) {
            result.add(new String(string));
            return;
        }
        // chars[index] 2 5 6
        char[] letters = lettersArray[chars[index] - '2'];
        for (char letter : letters) {
            string[index] = letter;
            dfs(index + 1, result, chars, string);
        }
    }
}
